﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace schedule
{
    class Program
    {
        IDEQueue<int> queue;
        Random random;

        public IDEQueue<int> QUEUE
        {
            get { return queue; }
            set { queue = value; }
        }

        public Program()
        {
            random = new Random();
        }

        public void pushBtm()
        {
            while (true)
            {
                if ((queue as BoundedDEQueue<int>).isFull)
                {
                    System.Console.WriteLine("{0},  boundedQueue is full", Thread.CurrentThread.GetHashCode());
                    Thread.Sleep(10);
                    continue;
                }

                int i = random.Next(1, 10);
                System.Console.WriteLine("{1},  push bottom {0}, bottom: {2}, top: {3}", i, Thread.CurrentThread.GetHashCode(), 
                    (queue as BoundedDEQueue<int>).bottom, (queue as BoundedDEQueue<int>).top);
                queue.pushBottom(i);
                Thread.Sleep(500);
            }
        }

        public void popTop()
        {
            while (true)
            {
                //System.Console.WriteLine((queue.popTop() as Int32).ToString());
                if (queue.isEmpty())
                {
                    System.Console.WriteLine("{0}, queue is empty", Thread.CurrentThread.GetHashCode());
                    Thread.Sleep(100);
                }
                else
                {
                    queue.popTop();
                    System.Console.WriteLine("{0}, queue popTop, bottom: {1}, top: {2}", Thread.CurrentThread.GetHashCode(),
                        (queue as BoundedDEQueue<int>).bottom, (queue as BoundedDEQueue<int>).top);
                }
                Thread.Sleep(random.Next(500, 1000));
            }
        }

        public void popBom()
        {
            while (true)
            {
                if (queue.isEmpty())
                {
                    System.Console.WriteLine("{0}, queue is empty", Thread.CurrentThread.GetHashCode());
                    Thread.Sleep(10);
                }
                else
                {
                    int i = queue.popBottom();
                    System.Console.WriteLine("{0}, popBom {3}, bottom: {1}, top: {2}", Thread.CurrentThread.GetHashCode(),
                        (queue as BoundedDEQueue<int>).bottom, (queue as BoundedDEQueue<int>).top, i);
                }
                Thread.Sleep(random.Next(100, 500));
            }
        }

        static void Main(string[] args)
        {
            System.Console.WriteLine("-------schedule--------");

            Program p = new Program();
            p.QUEUE = new BoundedDEQueue<int>(3);

            Thread[] threadSet = new Thread[2];
            threadSet[1] = new Thread(new ThreadStart(p.popBom));
            //threadSet[2] = new Thread(new ThreadStart(p.popTop));
            threadSet[0] = new Thread(new ThreadStart(p.pushBtm));

            //foreach (Thread t in threadSet)
            //{
            //    t.Start();
            //    t.IsBackground = true;
            //}


            TaskDistributor taskDistributor = new TaskDistributor(2);

            for (int i = 0; i < 15; ++i)
            {
                taskDistributor.addTask(new TaskParams(i));
            }

            System.Console.WriteLine("Main thread ID is {0}", Thread.CurrentThread.GetHashCode());
            System.Console.WriteLine("------------------------------");
            while (!taskDistributor.finished)
            {
                //if(loopCount < 50)
                //    System.Console.WriteLine("updata called {0} times", ++loopCount);
                taskDistributor.updata();
            }

            System.Console.WriteLine("***********Tasks finished**************");

            //Thread thTest = new Thread(new ParameterizedThreadStart(TaskDistributor.defaultTaskOp));
            //thTest.Start(new TaskParams(-1));

            System.Console.ReadLine();
            

        }
    }
}
